const TOOLBAR = [
	{
		content: "批量修改",
		icon: "el-icon-edit-outline",
		type: "normal",
		ifshow: true
	},
	{
		content: "添加",
		icon: "el-icon-document-add",
		type: "primary",
		ifshow: true
	},{
		content: "状态设定",
		icon: "el-icon-s-claim",
		type: "normal",
		ifshow: true
	},{
		content: "事项转移",
		icon: "el-icon-document-update",
		type: "normal",
		ifshow: true
	},
	{
		content: "删除",
		icon: "el-icon-document-remove",
		type: "danger",
		ifshow: false
	}
];
const keepaccountFormRules = {
	customerId: [{
		required: true,
		message: '请选择客户公司名称',
		trigger: 'blur'
	}],
	contractNumber: [{
		required: true,
		message: '请选择合同号',
		trigger: 'blur'
	}],
	documentNumber: [{
		required: true,
		message: '请选择档案号',
		trigger: 'blur'
	}],
	taxGroup: [{
		required: true,
		message: '请选择财税分组',
		trigger: 'blur'
	}],
	TCP: [{
		required: true,
		message: '请选择是否有税控盘',
		trigger: 'blur'
	}],
	renew: [{
		required: true,
		message: '请选择续费情况',
		trigger: 'blur'
	}],
	leaderId: [{
		required: true,
		message: '请选择财税负责人',
		trigger: 'blur'
	}],
	enddate: [{
		required: true,
		message: '请选择截止日期',
		trigger: 'blur'
	}],
	amount: [{
		required: true,
		message: '请选择数量',
		trigger: 'blur'
	}],
	unit: [{
		required: true,
		message: '请选择单位',
		trigger: 'blur'
	}],
	remake: [{
		required: true,
		message: '请填写备注',
		trigger: 'blur'
	}],
	chargerId: [{
		required: true,
		message: "事项负责人不能为空",
		trigger: "blur"
	}]

};
const serviceFormDetailRules = {
	name: [{
		required: true,
		message: '请选择服务名称',
		trigger: 'blur'
	}],
	time: [{
		required: true,
		message: '请输入时限',
		trigger: 'blur'
	}],
	amount: [{
		required: true,
		message: '请输入数量',
		trigger: 'blur'
	}],
	unit: [{
		required: true,
		message: '请输入单位',
		trigger: 'blur'
	}],
	cost: [{
		required: true,
		message: '请输入成本金额',
		trigger: 'blur'
	}],
	price: [{
		required: true,
		message: '请输入单价',
		trigger: 'blur'
	}],
	totalprice: [{
		required: true,
		message: '请输入服务金额',
		trigger: 'blur'
	}]
};
const COLUMNS = [
	{
		prop: "id",
		label: "账本号",
		width: "80"
	},
	{
		prop: "contractNumber",
		label: "合同号",
		width: "80"
	},
	{
		prop: "documentNumber",
		label: "档案号",
		width: "80"
	},
	{
		prop: "taxGroupName",
		label: "财税组",
		width: "80"
	},
	{
		prop: "compname",
		label: "公司名称",
		width: "160"
	},
	{
		prop: "contacter",
		label: "联系人",
		width: "80"
	},
	{
		prop: "phone",
		label: "电话",
		width: "130"
	},
	{
		prop: "taxtype",
		label: "税务类型",
		width: "100"
	},
	{
		prop: "status",
		label: "状态",
		width: "80"
	},
	{
		prop: "leader",
		label: "负责人",
		width: "80"
	},
	{
		prop: "amount",
		label: "数量",
		width: "40"
	},
	{
		prop: "unit",
		label: "单位",
		width: "40"
	},
	{
		prop: "renew",
		label: "续费",
		width: "60"
	},
	{
		prop: "TCP",
		label: "税控盘",
		width: "70"
	},
	{
		prop: "sbgjj",
		label: "社保公积金",
		width: "100"
	},
	{
		prop: "enddate",
		label: "截止日期",
		width: "100"
	},
	{
		prop: "remake",
		label: "备注",
		width: "130"
	}
];

const transFormrules = {
	contractNumber: [{
		required: true,
		message: '请选择合同号',
		trigger: 'blur'
	}],
	documentNumber: [{
		required: true,
		message: '请选择档案号',
		trigger: 'blur'
	}],
	taxGroup: [{
		required: true,
		message: '请选择财税分组',
		trigger: 'blur'
	}],
	amount: [{
		required: true,
		message: '请输入数量',
		trigger: 'blur'
	}],
	unit: [{
		required: true,
		message: '请选择年月单位',
		trigger: 'blur'
	}],
	TCP: [{
		required: true,
		message: '请选择是否有税控盘',
		trigger: 'blur'
	}],
	renew: [{
		required: true,
		message: '请选择续费情况',
		trigger: 'blur'
	}],
	enddate: [{
		required: true,
		message: '请选择服务到期时间',
		trigger: 'blur'
	}],
	leaderId: [{
		required: true,
		message: '请选择财税负责人名字',
		trigger: 'blur'
	}]
};
const updateFormrules ={
	matter: [{
		required: true,
		message: '请选择事项',
		trigger: 'blur'
	}],oldchargerId: [{
		required: true,
		message: '请选择需要替换的负责人',
		trigger: 'blur'
	}],newchargerId: [{
		required: true,
		message: '请选择新的负责人',
		trigger: 'blur'
	}],
};
const updateTypeFormrules ={
	typeS: [{
		required: true,
		message: '请选择替换的类型',
		trigger: 'blur'
	}],typeSS: [{
		required: true,
		message: '请选择替换的新类型',
		trigger: 'blur'
	}],
};
const statusFormrules ={
	status: [{
		required: true,
		message: '请选择状态',
		trigger: 'blur'
	}],
};
const customerFormrules = {
	compname: [{
		required: true,
		message: '请输入客户名称',
		trigger: 'blur'
	}],
	contacter: [{
		required: true,
		message: '请输入联系人姓名',
		trigger: 'blur'
	}],
	contractNumber: [{
		required: true,
		message: '请输入联系人电话',
		trigger: 'blur'
	}],

	taxtype: [{
		required: true,
		message: '请选择税务类型',
		trigger: 'blur'
	}]
};
const keepaccountDetailFormrules = {
	matter: [{
		required: true,
		message: '请选择事项',
		trigger: 'blur'
	}],
	status: [{
		required: true,
		message: '请选择状态',
		trigger: 'blur'
	}],
	enddate: [{
		required: true,
		message: '请选择截止日期',
		trigger: 'blur'
	}],
	chargerId: [{
		required: true,
		message: '请选择事项负责人',
		trigger: 'blur'
	}]
}

let app = new Vue({
	el: "#app",

	data() {
		return {
			options: [],
			value: [],
			list: [],
			loading: false,
			serviceCheckForm: {

			},
			toolbar: TOOLBAR,
			imgdialogImageUrl: false,
			imgdialogVisible: false,
			dialogFormVisible: false,
			uploadurl: axios.defaults.baseURL + "api/keepaccountdetailform/annex/upload/",
			headers: {
				Token: localStorage.getItem('TOKEN'),
				UserID: localStorage.getItem('USERID')
			},
			fileList: [],
			//员工
			allusers: [],
			submitDisable: false,
			combinationForm: {
				defaultProps: {
					children: 'children',
					label: 'label'
				},
			},
			stateoptions: STATETYPE,
			teamLink: "i am copy",
			leftbar_show: true,
			// 添加、编辑标识
			action: "",
			docsrc:"",
			dialogDocFormVisible: false,
			dialogserviceFormDetailVisible: false,
			dialogEditFormVisible: false,
			dialogEditFormTypeVisible: false,
			dialogEditBaseFormVisible: false,
			dialogItemFormVisible: false,
			dialogUpdateFormVisible:false,
			dialogstatusFormVisible:false,
			tableHeight: window.innerHeight - 180,
			//dialog tab
			tabvalue: "base",
			tabdistributevalue: "base",
			//服务项目列表
			services: [],
			serviceAgent: [],
			tablecolums: COLUMNS,
			updateForm:{},
			updateTypeForm:{},
			statusForm:{},
			queryForm: {
			    distinct: "不显示",
			    taxGroup: "",
			    taxtype: "",
			    TCP: "",
			    sbgjj: "",
				renew: "",
				documentNumber: "",
				contractNumber: "",
				userid: localStorage.getItem('USERID'),
				keyword: "",
				taxtype: "",
				name: AGENTKEEPNAME,
				matter: "",
				month: "",
				status: "",
				personnel: "",
				endTime: [],
				createTime: [],
				updateTime: [],
				pageNo: 1, // 当前页码
				pagesize: 20, // 每页记录数
				pagesizes: [10, 20, 50, 100]
			},
			transForm: {

			},
			customerForm: {

			},
			accountDetailstableForm: [],

			keepaccountDetailForm: {

			},
			keepaccountForm: {
				retry:'否'
			},
			detailcolums: [{
					"prop": "id",
					"label": "编号",
					"width": "100"
				},
				{
					"prop": "name",
					"label": "名称",
					"width": "100"
				},
				{
					"prop": "spec",
					"label": "规格",
					"width": "80"
				},
				{
					"prop": "type",
					"label": "类别",
					"width": "80"
				},
				{
					"prop": "unit",
					"label": "单位",
					"width": "50"
				},

				{
					"prop": "price",
					"label": "单价",
					"width": "100"
				},
				{
					"prop": "amount",
					"label": "数量",
					"width": "50"
				},
				{
					"prop": "totalprice",
					"label": "收款金额",
					"width": "100"
				},
				{
					"prop": "cost",
					"label": "成本金额",
					"width": "100"
				},
				{
					"prop": "items",
					"label": "服务项目",
					"width": "80"
				},
				{
					"prop": "memo",
					"label": "备注",
					"width": "80"
				}
			],
			distridetailcolums: [{
				"prop": "name",
				"label": "事项名称",
				"width": "80"
			}],
			serviceForm: {
				id: null,
				mycustomerid: null,
				customerid: null,
				compname: "",
				legalperson: "",
				contacter: "",
				phone: "",
				address: "",
				area: "",
				status: "",
				business: "",
				collectiondate: "",
				transdate: "",
				treasurer: "",
				supervisor: "",
				sellerid: "",
				sellername: "",
				outformerid: "",
				outformername: "",
				src: "",
				signtype: "",
				addresstype: "",
				invoicingtype: "",
				amountinvoicing: "",
				taxtype: "",
				gifts: "",
				paytype: "",
				service: [],
				amounttotal: 0,
				amountreceivables: "",
				amountpayin: "",
				amountend: "",
				amountendcondition: "",
				financeid: "",
				financename: "",
				financestatus: "",
				materials: [],
				annex: [],
				handoverid: "",
				handovername: "",
				handoverstatus: "",
				receiverid: "",
				receivername: "",
				receiverstatus: "",
				supervisorid: "",
				supervisorname: "",
				supervisorstatus: "",
				costerid: "",
				costername: "",
				costerstatus: "",
				memo: ""
			},
			serviceFormDetail: {
				id: 0,
				serviceId: 0,
				name: "",
				spec: "",
				type: "",
				amount: 0,
				unit: "",
				cost: 0,
				price: 0,
				totalprice: 0,
				memo: "",
				service: []
			},
			addBusinessShow: true,
			pagepersontotal: 0,
			pagepersontotaltemp: 0,
			pagetotal: 0,
			pagetotaltemp: 0,
			pickerOptions: {
				shortcuts: [{
					text: '最近一周',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近一个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
						picker.$emit('pick', [start, end]);
					}
				}, {
					text: '最近三个月',
					onClick(picker) {
						const end = new Date();
						const start = new Date();
						start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
						picker.$emit('pick', [start, end]);
					}
				}]
			},
			//税务类型
			taxtypeoptions: TAXTYPEOPTIONS,
			//状态
			status: WORKORDERSTATUS,
			// 所有部门
			depts: [],
			//年月单位
			units: [{
				value: '年',
				label: '年'
			}, {
				value: '月',
				label: '月'
			}],
			//员工
			members: [],
			//服务名称
			servicenames: [],
			//待办事项
			handleitems: [
				'代理记账',
				'资料收集',
				'社保、公积金',
				'抄税 小规模季度',
				'国税 小规模季度',
				'个税 每月',
				'清卡 每月'
			],
			// 托管选项
			TCPS: [
				'无',
				'自开',
				'未申领',
				'已托管'
			],
			//社保公积金
			sbgjjs: [
				'无',
				'社保',
				'公积金',
				'社保+公积金'
			],
			// 多选
			multipleSelection: [],
			pagedatatemp: [], // 当前页结果集
			pagedata: [], // 当前页结果集
			pagepersondatatemp: [], // 当前页用户结果集
			pagepersondata: [], // 当前页用户结果集
			loaded: false,
		}
	},



	methods: {

		showRow(row, rowIndwx) {
			let styleJson = {}
			if (row.show) {
				styleJson = {
					'margin-left': '0px'
				}
			} else {
				styleJson = {
					'margin-left': '0px'
				}
			}
			return styleJson // 返回对象
		},
		//点击代理记账某条信息，明细表筛选过滤对应的月份用来做筛选
		clickTable(row, expandedRows) {
			var map = {}
			var dest = []
			item_one = row.recs
			var dest = []
			var map = {}

			for (index in item_one) {
				item = item_one[index]
				if (!map[item.month]) {
					dest.push({
						month: item.month,
					});
					map[item.month] = item
				} else {

				}
			}
			var Form = []
			for (index in dest) {
				Form.push({
					text: dest[index].month,
					value: dest[index].month
				})
			}
			this.filtersMonth = Form
		},

		// 通过公司名称查询公司ID
		async querycustomer(query) {

			try {
				let res = await axios.get('api/keepaccount/querycustomer/' + query);

				if (res.code === 200) {
					this.list = res.data
					//this.$message.success(res.message);
				} else {
					//this.$message.error(res.message);
				}
			} catch (e) {
				//this.$message.error('请求出错！', e.message);
			}


		},
		//远程查询公司ID方法
		remoteMethod(query) {
			if (query.length > 3) {
				this.querycustomer(query)
			}
			if (query !== '') {
				this.loading = true;
				setTimeout(() => {
					this.loading = false;
					this.options = this.list.filter(item => {
						return item.label.toLowerCase()
							.indexOf(query.toLowerCase()) > -1;
					});
				}, 1000);
			} else {
				this.options = [];
			}
		},

		// 添加服务
		addService() {
			this.action = "adddetail";
			this.dialogserviceFormDetailVisible = true
			this.serviceFormDetail.id = 0
			this.serviceFormDetail.serviceId = 0
			this.serviceFormDetail.name = ""
			this.serviceFormDetail.amount = 0
			this.serviceFormDetail.unit = ""
			this.serviceFormDetail.cost = 0
			this.serviceFormDetail.price = 0
			this.serviceFormDetail.totalprice = 0
			this.serviceFormDetail.memo = ""
			this.serviceFormDetail.service = []
		},
		// 编辑服务
		editService(index, row) {
			this.action = "editdetail"
			console.log(row)
			this.dindex = index;
			this.dialogserviceFormDetailVisible = true
			this.serviceFormDetail = deepCopy(row)
		},
		// 删除服务
		delService(index, row) {
			this.serviceForm.service.splice(index, 1)
			this.keepaccountForm.recs.splice(index, 1)
			if (this.keepaccountForm.recs.length == 0) {
				this.addBusinessShow = true
			}
		},
		// 服务项目变化时
		async servicechange(id) {
			// 显示加载框
			console.log(id)
			const loading = this.$loading(LOAD_STYLE);
			try {

				let res = await axios.get('api/servicedetail/get/' + id);
				console.log(res)
				if (res.code === 200) {
					this.serviceFormDetail.service = res.data;
					if (this.serviceFormDetail.service.length > 0) {
						this.serviceFormDetail.amount = 1
						this.serviceFormDetail.serviceId = id
						this.serviceFormDetail.id = this.serviceFormDetail.service[0].id
						this.serviceFormDetail.name = this.serviceFormDetail.service[0].name
						this.serviceFormDetail.price = this.serviceFormDetail.service[0].price
						this.serviceFormDetail.totalprice = this.serviceFormDetail.service[0].price * this.serviceFormDetail.amount
						this.serviceFormDetail.unit = this.serviceFormDetail.service[0].unit
						this.serviceFormDetail.spec = this.serviceFormDetail.service[0].spec
						this.serviceFormDetail.type = this.serviceFormDetail.service[0].type

						this.serviceFormDetail.cost = this.serviceFormDetail.service[0].cost * this.serviceFormDetail.amount
						this.serviceFormDetail.items = this.serviceFormDetail.service[0].items
						this.serviceFormDetail.memo = this.serviceFormDetail.service[0].memo
					} else {
						this.serviceFormDetail.serviceId = 0
						this.serviceFormDetail.id = 0
						this.serviceFormDetail.amount = 1
						this.serviceFormDetail.name = this.serviceFormDetail.name
						this.serviceFormDetail.price = 0
						this.serviceFormDetail.totalprice = 0
						this.serviceFormDetail.unit = "无"
						this.serviceFormDetail.spec = "无"
						this.serviceFormDetail.type = "无"
						this.serviceFormDetail.cost = 0
						this.serviceFormDetail.items = ""
						this.serviceFormDetail.memo = ""
					}
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('服务项目请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
		},
		//代理确认
		agentconfirm() {
			this.$refs.keepaccountForm.validate(async valid => {
				if (valid) {
					this.keepaccountForm.amount = parseInt(this.keepaccountForm.amount)
					// 显示加载框
					const loading = this.$loading(LOAD_STYLE);
					try {
						// 代理记账录入确认
						let res = await axios.post('api/keepaccount/add', this.keepaccountForm);
						if (res.code === 200) {
							this.$message.success(res.message);
							this.query();
							this.dialogFormVisible = false
						} else {
							this.$message.error(res.message);
						}
					} catch (e) {
						this.$message.error('添加出错！', e.message);
					}
					//关闭加载框
					loading.close();


				} else {
					if (this.keepaccountForm.recs.length == 0) {
						this.$message.error('请在选择业务中添加服务项目');
						this.tabvalue = 'base'
					} else if (this.keepaccountForm.recs.length > 1) {
						this.$message.error('请在办理业务中删除服务项目，只能选择一个');
						this.tabvalue = 'base'
					} else {
						this.$message.error('请确认基础信息，负责人员*号项目都以填写。');
					}
				}
			})

		},
		// 确认添加，编辑服务信息
		confirmService() {
			this.$refs.serviceFormDetail.validate(async valid => {
				if (valid) {
					let res = null;
					if (this.action == "adddetail") {
						// 添加服务
						this.serviceForm.service.push(deepCopy(this.serviceFormDetail))
						this.keepaccountForm.recs.push(deepCopy(this.serviceFormDetail))
					} else if (this.action == "editdetail") {
						// 编辑服务
						this.serviceForm.service[this.dindex].amount = this.serviceFormDetail.amount
						this.serviceForm.service[this.dindex].totalprice = this.serviceFormDetail.totalprice
						this.serviceForm.service[this.dindex].name = this.serviceFormDetail.name
						this.serviceForm.service[this.dindex].unit = this.serviceFormDetail.unit
						this.serviceForm.service[this.dindex].time = this.serviceFormDetail.time
						this.serviceForm.service[this.dindex].cost = this.serviceFormDetail.cost
						this.serviceForm.service[this.dindex].price = this.serviceFormDetail.price
						this.keepaccountForm.amount = this.serviceFormDetail.amount
						this.keepaccountForm.unit = this.serviceFormDetail.unit
						this.keepaccountForm.recs[this.dindex].price = this.serviceFormDetail.price
					}
					if (this.keepaccountForm.recs.length > 0) {
						this.addBusinessShow = false
					}
					this.keepaccountForm.servicedetailId = this.keepaccountForm.recs[0].id
					this.keepaccountForm.name = this.keepaccountForm.recs[0].name
					this.keepaccountForm.unit = this.keepaccountForm.recs[0].unit
					this.keepaccountForm.amount = this.keepaccountForm.recs[0].amount
					this.dialogserviceFormDetailVisible = false
				} else {

				}
			})
		},
		async removesummary() {
			try {
				let res = await axios.post('api/keepaccount/removesummary', this.transForm);
				if (res.code === 200) {
					this.query();
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				} else {
					this.$message.error(res.message);
				}

			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

		},


		async removeitem() {
			try {
				let res = await axios.post('api/keepaccount/removeitem', this.keepaccountDetailForm);
				if (res.code === 200) {
					this.query();
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				} else {
					this.$message.error(res.message);
				}

			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

		},
		//删除
		remove(index, row) {
			this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				if (row["recs"] != undefined) {
					this.transForm = deepCopy(row)
					this.removesummary()
				} else if (row["recs"] == undefined) {
					this.keepaccountDetailForm = deepCopy(row)
					this.removeitem()
				}

			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},
		additem(index, row) {
			this.action = "additem"
			this.index = index;
			this.dialogItemFormVisible = true;
			this.fileList = [];
			this.keepaccountDetailForm = {
				keepaccountId: row.id,
				charger: null,
				matter: "",
				status: "",
				enddate: "",
				leader: "",
				remake: "",
				evidence: []
			}
		},
		edititem(index, row, n) {
			this.action = "edititem"
			this.dialogItemFormVisible = true;
			this.keepaccountDetailForm = deepCopy(row.data[n])
			// 保留原始数据，方便比对
			this.keepaccountDetailForm = deepCopy(row.data[n])
			this.$set(this.keepaccountDetailForm, "enddate", row.month)
			this.fileList = this.keepaccountDetailForm.evidence
			for (item in this.keepaccountDetailForm.evidence) {
				this.fileList[item].url = DOWNLOAD_KEEPACCOUNT_PATH + "/" + this.keepaccountDetailForm.keepaccountid + "/" + this
					.keepaccountDetailForm.id + "/" + this.keepaccountDetailForm.evidence[item].name + "/" + localStorage.getItem(
						'TOKEN')
			}
			this.createTempfilelist(this.fileList)
		},
		// 取得所有部门列表
		async getAllGroups() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/allgroups');
				if (res.code === 200) {
					this.depts = res.data;
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
		},
		async itemcofirm() {
			this.submitDisable = true;
			this.$refs.keepaccountDetailForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = null;
						if (this.action == "additem") {
							//添加
							res = await axios.post('api/keepaccount/additem', this.keepaccountDetailForm);
						} else if (this.action == "edititem") {
							//编辑				
							res = await axios.put('api/keepaccount/edititem', this.keepaccountDetailForm);
						}

						if (res.code === 200) {
							this.dialogItemFormVisible = false
							this.query()
							this.$message.success(res.message);
						} else {
							this.$message.error(res.message);
						}

					} catch (e) {
						this.$message.error('请求出错！', e.message);
					}

					// 关闭加载框
					loading.close();
				}
			})
			this.submitDisable = false;
		},
		docshow(index, row) {
			this.action = "docshow"
			this.index = index;
			this.dialogDocFormVisible = true;
			this.customerForm = deepCopy(row)
			this.docsrc = "../service/uploadframe.html?docnumber=" + this.customerForm.documentNumber
		},
		editbase(index, row) {
			this.action = "editbase"
			this.index = index;
			this.dialogEditBaseFormVisible = true;
			this.customerForm = deepCopy(row)
		},
		edit(index, row) {
			console.log(row)
			this.action = "edit"
			this.index = index;
			this.dialogEditFormVisible = true;
			row.taxGroup = row.taxGroup == 0 ? null : row.taxGroup
			this.transForm = deepCopy(row)
			this.transForm.members = [{
				name: row.recs[0].matter,
				chargerId: row.recs[0].chargerId
			}, {
				name: row.recs[1].matter,
				chargerId: row.recs[1].chargerId
			}, {
				name: row.recs[2].matter,
				chargerId: row.recs[2].chargerId
			}, {
				name: row.recs[3].matter,
				chargerId: row.recs[3].chargerId
			}, {
				name: row.recs[4].matter,
				chargerId: row.recs[4].chargerId
			}, {
				name: row.recs[5].matter,
				chargerId: row.recs[5].chargerId
			}]

		},
		async editbaseconfirm() {
			this.$refs.customerForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = await axios.put('api/keepaccount/editcus', this.customerForm);
						if (res.code === 200) {
							this.dialogEditBaseFormVisible = false
							this.query()
							this.$message.success(res.message);
						} else {
							this.$message.error(res.message);
						}
					} catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
				}
			})
		},
		async editcofirm() {

			// 显示加载框
			this.$refs.transForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = await axios.put('api/keepaccount/edit', this.transForm);
						if (res.code === 200) {
							this.dialogEditFormVisible = false
							this.query()
							this.$message.success(res.message);
						} else {
							this.$message.error(res.message);
						}
					} catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
				}
			})
		},
		// 初始化团队成员列表
		async initMembers() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/team/member/' + localStorage.getItem('USERID'));
				if (res.code === 200) {
					this.members = res.data;
					if (this.members.length > 0) {

					}
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
		},
		async initServiceName() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/service/servicename');
				if (res.code === 200) {
					this.servicenames = res.data;
					if (this.servicenames.length > 0) {

					}
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
		},


		// 取得组织结构
		async getCombination() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/detp/combination');
				if (res.code === 200) {
					this.combinationForm.data = res.data;
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('组织结构请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
		},

		toolAction(acName) {
			if (acName == "删除") {
				this.removeconfirm();
			} else if (acName == "添加") {
				this.addconfirm();
			}else if(acName=="事项转移"){
				this.batchupdate();
			}else if(acName=="批量修改"){
				this.updatetype();
			}else if(acName=="状态设定"){
				this.statusset();
			}
		},
		// 状态设定
		statusset(){
			if (this.multipleSelection.length == 0) {
				this.$message.error('请选择数据行！');
				return;
			}
			tempkeepaccountids=[]
			for (let row of this.multipleSelection) {
				tempkeepaccountids.push(row.id)
			}
			console.log(tempkeepaccountids)
			this.statusForm={
				memo:"",
				status:'',
				keepaccountIds:tempkeepaccountids,
			}
			this.dialogstatusFormVisible =true;
		},
		updatetype() {
		    if (this.multipleSelection.length == 0) {
				this.$message.error('请选择数据行！');
				return;
			}

			tempkeepaccountids=[]
			for (let row of this.multipleSelection) {
				tempkeepaccountids.push(row.id)
			}
			console.log(tempkeepaccountids)
			this.updateTypeForm={
				typeS:["税务类型","税控盘","社保公积金"],
				typeSX:'',
				typeSSX:'',
				税务类型:["小规模","一般纳税人","个体查帐","个体定期定额"],
				税控盘:["UK托管","黑盘托管","白盘托管","托管无盘","有盘不托管","无盘"],
				社保公积金:["公积金","社保和公积金","无"],
				keepaccountIds:tempkeepaccountids,
			}
			this.dialogEditFormTypeVisible =true;
		},
		async updateTypeBatch() {
		    console.log(this.updateTypeForm);

		    // 显示加载框
            const loading = this.$loading(LOAD_STYLE);
            try {
                let res = await axios.put('api/keepaccount/updateTypeBatch', this.updateTypeForm);
                if (res.code === 200) {
                    this.startType();
                    this.query();
                    this.$message.success(res.message);
                } else {
                    this.$message.error(res.message);
                }
            } catch (e) {
                this.$message.error('修改出错！', e.message);
            }
            // 关闭加载框
            loading.close();

            this.dialogEditFormTypeVisible = false;
		},
		batchupdate(){
			if (this.multipleSelection.length == 0) {
				this.$message.error('请选择数据行！');
				return;
			}
			tempkeepaccountids=[]
			for (let row of this.multipleSelection) {
				tempkeepaccountids.push(row.id)
			}
			console.log(tempkeepaccountids)
			this.updateForm={
				matter:[],
				oldchargerId:null,
				newchargerId:null,
				keepaccountIds:tempkeepaccountids,
				status:''
			}
			this.dialogUpdateFormVisible =true;

		},
		// 状态设定
		statussetconfirm(){
				// 显示加载框
			this.$refs.statusForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = await axios.put('api/keepaccount/statusset', this.statusForm);
						if (res.code === 200) {
							this.dialogstatusFormVisible = false
							this.query()
							this.$message.success(res.message);
						} else {
							this.$message.error(res.message);
						}
					} catch (e) {
						this.$message.error('状态设定请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
				}
			})
		},
		async batchupdateconfirm(){
				// 显示加载框
			this.$refs.updateForm.validate(async valid => {
				if (valid) {
					const loading = this.$loading(LOAD_STYLE);
					try {
						let res = await axios.put('api/keepaccount/batchnewcharger', this.updateForm);
						if (res.code === 200) {
							this.dialogUpdateFormVisible = false
							this.query()
							this.$message.success(res.message);
						} else {
							this.$message.error(res.message);
						}
					} catch (e) {
						this.$message.error('请求出错！', e.message);
					}
					// 关闭加载框
					loading.close();
				}
			})
		},
		addconfirm() {
			this.action = "add";
			this.dialogFormVisible = true;
			this.keepaccountForm = {
				enddate: "",
				name: "",
				servicedetailId: null,
				leaderId: null,
				remake: "",
				amount: "",
				unit: "",
				recs: [],
				retry:'否',
				members: [{
					name: '资料收集',
					chargerId: null
				}, {
					name: '社保、公积金',
					chargerId: null
				}, {
					name: '抄税 小规模季度',
					chargerId: null
				}, {
					name: '国税 小规模季度',
					chargerId: null
				}, {
					name: '个税 每月',
					chargerId: null
				}, {
					name: '清卡 每月',
					chargerId: null
				}],
				customerId: null
			}
			this.serviceForm.service = []
			this.addBusinessShow = true
		},
		removeconfirm() {
			if (this.multipleSelection.length > 0) {
				if (true) {
					this.$confirm('选择行中有多条数据，确定要将数据删除吗?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(async () => {
						this.removemul()
					}).catch((e) => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						});
					});
				} else {

				}
			} else {
				this.$message.error('请选择数据行！');
			}
		},
		async removemul() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let ids = []
				for (let row of this.multipleSelection) {
					ids.push(row.id)
				}
				let param = {
					"ids": ids
				}
				let res = await axios.post('api/keepaccount/removemul', param);
				if (res.code === 200) {
					this.$message.success(res.message);
					this.query();
					//this.dialogAssigneFormVisible = false
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}
			// 关闭加载框
			loading.close();
		},

		// 显示侧边栏
		showLeftBar() {

			if (this.leftbar_show)
				return this.leftbar_show = false
			if (!this.leftbar_show)
				return this.leftbar_show = true

		},
		// 表格多选 
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},


		start() {
			this.queryForm.pageNo = 1
			this.query()
		},

		// 查询
		async query() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/keepaccount/querylist', this.queryForm);
				if (res.code === 200) {
					this.pagedata = res.data;
					this.pagetotal = res.total;
					var endTime = new Date();
					this.transdata(this.pagedata);
					this.$message.success(res.message);
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();
			var lastendTime = new Date();
		},
		transdata(transdata) {
			this.accountDetailstableForm = []
			transdata.map((item, index) => {
				this.accountDetailstableForm.push({
					leaderId: item.leaderId,
					data: Object.assign(item.recs)
				})
			});
			var map = {}
			var dest = []
			var tempForm = []
			for (index_one in this.accountDetailstableForm) {
				item_one = this.accountDetailstableForm[index_one].data
				item_leader = this.accountDetailstableForm[index_one].leaderId
				var dest = []
				var map = {}
				for (index in item_one) {
					item = item_one[index]
					if (!map[item.month]) {
						dest.push({
							leaderId: item_leader,
							month: item.month,
							data: [Object.assign(item)]
						});
						map[item.month] = item
					} else {
						for (index in dest) {
							var dj = dest[index]
							if (dj.month == item.month) {
								dj.data.push(
									Object.assign(item)
								);
								break;
							}
						}
					}
				}
				tempForm.push(
					dest
				);
			}
			this.accountDetailstableForm = tempForm
		},
		selectionChange(selection) {
			this.selection = selection
		},
		// 设定表格当前页
		handleCurrentPageChange(selectedPageNo) {
			this.queryForm.pageNo = selectedPageNo
			// 加载数据页
			this.query()
		},
		//设定每页记录数
		handleSizeChange(val) {
			this.queryForm.pagesize = val
			// 加载数据页
			this.query()
		},
		// 设定表格当前页
		handlePersonCurrentPageChange(selectedPageNo) {
			this.queryPersonForm.pageNo = selectedPageNo
			// 加载数据页
			this.queryPerson()
		},
		//设定每页记录数
		handlePersonSizeChange(val) {
			this.queryPersonForm.pagesize = val
			// 加载数据页
			this.queryPerson()
		},
		handleRemove(file, fileList) {
			this.createTempfilelist(fileList);
		},
		handledownloadPreview(file) {
			this.imgdialogImageUrl = file.url;
			this.imgdialogVisible = true;
		},
		//upload系列
		handleAvatarSuccess(res, file, filelist) {
			this.createTempfilelist(filelist);
		},
		beforeAvatarUpload(file) {
			const isJPG = ["image/jpeg", "image/jpg", "image/png"].indexOf(file.type) > -1;
			const isLt2M = file.size / 1024 / 1024 < 2;
			if (!isJPG) {
				this.$message.error('图片只能是png,jpeg,jpg格式!');
				return false;
			}
			if (!isLt2M) {
				this.$message.error('上传图片大小不能超过 2MB!');
				return false;
			}
			return true;
		},
		createTempfilelist(filelist) {
			this.tempfilelist = []
			for (let s of filelist) {
				if (typeof(s.response) != "undefined") {
					this.tempfilelist.push({
						url: "",
						name: s.name,
					});
				} else {
					this.tempfilelist.push({
						url: "",
						name: s.name,
					});
				}
				this.keepaccountDetailForm.evidence = this.tempfilelist
			}
		},
		serviceDetailClick(row, column, event) {
			this.serviceFormDetail.id = row.id
			this.serviceFormDetail.amount = 1
			this.serviceFormDetail.name = row.name
			this.serviceFormDetail.price = row.price
			this.serviceFormDetail.unit = row.unit
			this.serviceFormDetail.spec = row.spec
			this.serviceFormDetail.type = row.type
			this.serviceFormDetail.cost = row.cost
			this.serviceFormDetail.items = row.items
			this.serviceFormDetail.memo = row.memo
		},
		filterHandler(value, row, column) {
			//列名 此处为month
			const property = column['property'];
			return row[property] === value;
		},

		async getAllUser() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/user/all');
				if (res.code === 200) {
					this.allusers = res.data;
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('取得所有用户列表请求出错！', e.message);
			}

			// 关闭加载框
			loading.close();

		},
		// 取得所有服务项目
		async getService() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.get('api/service/get');
				if (res.code === 200) {
					this.services = res.data;
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('服务项目请求出错！', e.message);
			}
			var result = this.services.filter(function(item) {
				return item.name == AGENTKEEPNAME
			});
			for (var i = 0; i < result.length; i++) {
					// console.log(result[i]);

			}
			this.services = result

			// 关闭加载框
			loading.close();
		},

		async init() {
			//初始化所有员工列表
			this.getAllUser();
			//初始化部门列表
			this.getAllGroups();
			//初始组织结构
			this.getCombination()
			//初始化员工列表
			this.initMembers();
			// 取得服务列表
			this.getService();
			//初始化服务名称
			await this.initServiceName();
			//初始化类型
			this.startType();

		}
	},
	async mounted() {
		// 自动调整表格高度
		window.onresize = () => {
				return (() => {
					this.tableHeight = window.innerHeight - 180
				})()
		},
		await this.init();
		for (toolindex in TOOLBAR) {
			if ((localStorage.getItem('ISLEADER') == '是' && localStorage.getItem('DEPTNAME') == '会计部') || localStorage.getItem(
					'ISMANAGER') == '是') {
				TOOLBAR[toolindex].ifshow = true
			}
		}
	}

});
